local blaze = require "blaze"
local app = require "app"
local game = app.game.running
local consts = game.util.consts
local log = blaze.logging.get("BetAreas")

local M = game.ui.element()

M.store_watchers = {
    gameplay = {
        ["阶段变更"] = function(self, store, state, args, init)
            if state.phase_id == consts.PHASE_SETTLE and state.result_view then
                self:result(state.result_view.hits)
            else
                for _, area in pairs(self.items) do
                    area:on_hit(false)
                end
            end
        end
    }
}

function M:ctor(res)
    assert(res)
    self.res = res
    self.items = {}
end

function M:bet(id)
    local player = game.kbe.player()
    if not player then
        return
    end
    if not player.chip then
        log.error(string.format("%s对象上没有chip属性？", player.className))
        return
    end
    local state = game.store.bet_amounts.state
    if not state.amount then
        log.error(string.format("没有下注金额？state=%s", blaze.dump(state)))
        return
    end
    if player.chip < state.amount then
        return
    end
    game.kbe.player():bet(id, state.amount)
end

function M:on_load()
    self:super("on_load")
    local BetArea = require "club.threecard.baccaratlib.ui.gameplay.BetArea"
    for _, config in ipairs(game.config.bet_areas.rows) do
        local area_trans = self.transform:Find(tostring(config.id))
        if area_trans then
            local item = BetArea.new(config, self)
            item:set_gameobject(area_trans.gameObject, true)
            self.items[item.id] = item
        else
            log.warn(string.format("找不到下注区域的游戏对象：%s", config.id))
        end
    end
end

function M:on_click(area)

end

function M:on_enter(area)
    if not self.pressed then
        return
    end

    self.last_enter_area = area

    local area_config = area.config
    for _, id in ipairs(area_config.nums) do
        self.items[id]:on_select(true)
    end
end

function M:on_exit(area)
    if not self.pressed then
        return
    end

    if self.last_enter_area == area then
        self.last_enter_area = nil
    end

    local area_config = game.config.bet_areas.get_by_id(area.id)
    for _, id in ipairs(area_config.nums) do
        self.items[id]:on_select(false)
    end
end

function M:on_up(area)
    if self.last_enter_area then
        self:bet(self.last_enter_area.id)
        self:on_exit(self.last_enter_area)
    end

    self.pressed = false
end

function M:on_press(area)
    local state = game.store.gameplay.state
    if state.phase_id ~= consts.PHASE_BET then
        return
    end

    self.pressed = true
    self:on_enter(area)
end

function M:on_release()
    self:super("on_release")
    for _, item in pairs(self.items) do
        item:on_release()
    end
end

function M:result(hit_ids)
    -- TODO:统一result显示
    if type(hit_ids) == "number" then
        hit_ids = {hit_ids}
    end

    for _, id in pairs(hit_ids) do
        if self.items[id] then
            self.items[id]:on_hit(true)
        end
    end
end

return M